﻿// sdl_dh2_v4.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
// 유니코드 주석 처리 확인

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{

	//The frames per second
	const int FRAMES_PER_SECOND = 30;

	//Keep Track of the current freame 
	int frame = 0;

	//Thre frame rate regulator
	Timer timer_fps;


	sdl_sub sdl_main;
	sdl_main.init("chem_hang_hae_si_dae", 480,640);

	sdl_text sdl_string;
	sdl_string.init("Daum_Regular.ttf", sdl_main.screen);

	image player("walk_ani.png", sdl_main.screen, 0,255,0, 8);
	image page_layout("page_layout.png", sdl_main.screen, 0,255,0);
	image bg_img("background.bmp", sdl_main.screen);

	SDL_Event event;
	bool quit = false;
	int p[4] = {0, 0, 0, 0};

	//시작 위치 설정
	player.set_xy.x = 345;
	player.set_xy.y = 320;
	bg_img.set_xy.x = -809;
	bg_img.set_xy.y = -592;

	//이동 가능한 화면영역 범위
	SDL_Rect playable;
	playable.x = 96;
	playable.y = 48;
	playable.h = 431;
	playable.w = 479;

	//레이아웃에 의한 맵 시작위치 조절
	bg_img.set_xy.y += playable.y;
	bg_img.set_xy.x += playable.x;

	Timer fps_check;
	fps_check.start();
	wchar_t * temp = L"0";
	int ani = 0;
	int sel = 0;
	//Start the frame timer
	while( quit == false)
	{
		
		//While there's an event to handle
		while(SDL_PollEvent(&event))
		{
			/////////종료처리/////////
			if(event.type == SDL_QUIT)
				quit = true;
			else if(event.type == SDL_KEYDOWN)
				if(event.key.keysym.sym == SDLK_ESCAPE)
					quit = true;

			////////이동처리/////////
			if(event.key.state == SDL_PRESSED)
			{
				if(event.key.keysym.sym == SDLK_UP)
				{
					p[0] = 5;
					sel = 2;
				}
				if(event.key.keysym.sym == SDLK_DOWN)
				{
					p[1] = 5;
					sel = 6;
				}
				if(event.key.keysym.sym == SDLK_LEFT)
				{
					p[2] = 5;
					sel = 4;
				}
				if(event.key.keysym.sym == SDLK_RIGHT)
				{
					p[3] = 5;
					sel = 0;
				}
			}
			if(event.key.state == SDL_RELEASED)
			{
				if(event.key.keysym.sym == SDLK_UP)
				{
					p[0] = 0;
				}
				if(event.key.keysym.sym == SDLK_DOWN)
				{
					p[1] = 0;
				}
				if(event.key.keysym.sym == SDLK_LEFT)
				{
					p[2] = 0;
				}
				if(event.key.keysym.sym == SDLK_RIGHT)
				{
					p[3] = 0;
				}
			}
		}
		
		//경계선 처리
		int line_feed = 0; //경계선 여유공간

		//키 입력에 의한 증감값 좌우나 상하를 동시에 입력하는 경우 이 값이 0이 되어 이동하지 않음
		int pre_move_y = p[1]-p[0];
		int pre_move_x = p[3]-p[2];

		if(player.set_xy.y +pre_move_y - line_feed >= playable.y && (player.set_xy.y +pre_move_y + player.offset.h)+ line_feed <= playable.h)
			player.set_xy.y = player.set_xy.y +(pre_move_y);
		else if(bg_img.set_xy.y -(pre_move_y) <= playable.y && bg_img.set_xy.y-pre_move_y+bg_img.offset.h >=  playable.h)
			bg_img.set_xy.y = bg_img.set_xy.y -(pre_move_y);
			
		if(player.set_xy.x +pre_move_x - line_feed >= playable.x && (player.set_xy.x +pre_move_x + player.offset.w)+ line_feed <= playable.w)
			player.set_xy.x = player.set_xy.x +(pre_move_x);
		else if(bg_img.set_xy.x-(pre_move_x) <=  playable.x && bg_img.set_xy.x-pre_move_x+bg_img.offset.w >= playable.w)
			bg_img.set_xy.x = bg_img.set_xy.x -(pre_move_x);

		bg_img.set(bg_img.set_xy.x, bg_img.set_xy.y);
		page_layout.set(0, 0);

		if(p[0] + p[1] + p[2] + p[3] > 0)
			ani = player.sprite_change(sel, 2, ani);

		player.set(player.set_xy.x, player.set_xy.y, ani);


		sdl_string.set(std::wstring(L"쳄항해시대 fps")+temp);
		sdl_main.view();
		std::cout <<" bx:"<<bg_img.set_xy.x<<" by:"<<bg_img.set_xy.y<<" ani:"<<ani<<" d:"<<sel;
		std::cout <<" x:"<<player.set_xy.x<<" y:"<<player.set_xy.y<<" up:"<<p[0]<<" down:"<<p[1]<<" left:"<<p[2]<<" right:"<<p[3];
		std::wcout <<" fps:"<<temp<<std::endl;
		temp = fps_check.RegulateFps(20);
	}
	return 0;
}
